home *** CD-ROM | disk | FTP | other *** search
/ Experimental BBS Explossion 3 / Experimental BBS Explossion III.iso / c / pcw.zip / PCWKERN.C < prev    next >
C/C++ Source or Header  |  1993-09-03  |  13KB  |  323 lines

  1. /***********************************************************/
  2. /* File Id.                  Pckern.C                      */
  3. /* Author.                   Stan Milam.                   */
  4. /* Date Written.             01/29/89.                     */
  5. /* Date Modified.            05/26/89.                     */
  6. /*                                                         */
  7. /*           (c) Copyright 1989-90 by Stan Milam           */
  8. /*                                                         */
  9. /* Modification Notes:  Rewrote initialization logic.  Add-*/
  10. /* ed global variables, _monitor, _adaptor, _video_ram.    */
  11. /* Added function to check video state & return max row &  */
  12. /* columns.                                                */
  13. /*                                                         */
  14. /* Comments:  This file will be the kernal of the Pcscrn   */
  15. /* library functions.  It will contain code to deal with   */
  16. /* the video environment.  Most important is the start up  */
  17. /* initialization code.  This code will determine the      */
  18. /* video adaptors on board.                                */
  19. /***********************************************************/
  20.  
  21. #ifdef MSC
  22. #  include <conio.h>
  23. #  define MK_FP(seg,off) ((void far *)(((long)(seg)<<16) | (off)))
  24. #  define outportb outp
  25. #  define inportb inp
  26. #define int86 _int86
  27. #define int86x _int86x
  28. #endif
  29.  
  30. #include <dos.h>
  31. #include <stdlib.h>
  32.  
  33. #define TRUE  1
  34. #define FALSE 0
  35.  
  36. #define COLORSEG  0XB800
  37. #define MONOSEG   0XB000
  38. #define MONO      0
  39. #define COLOR     1
  40.  
  41. #define MDA      1
  42. #define CGA      2
  43. #define EGA      3
  44. #define VGA      4
  45.  
  46. char PCWRKSTR[151];
  47.  
  48. void vgetmode(int *cols, int *mode, int *apage);
  49. void _pcw_exit(void);
  50.  
  51. /* Global Variables */
  52.  
  53. int      CheckSnow;                    /* Do we need to check for snow? */
  54. int      Vbump;                        /* Vertical increment in quick writes */
  55. int      _monitor;                     /* The kind of monitor we are using */
  56. int      _adaptor;                     /* The video adaptor we are using */
  57. int      _video_ram;                   /* How much video memory */
  58.  
  59. /*  Some pointers to low memory where vital info is to be found */
  60.  
  61. static char far *ega_byte;
  62. static int  far *regen_len;
  63. static char far *ega_rows;
  64. static char *_pcw_copyright;
  65.  
  66. /*  Values stored in this file to be used throughout the library */
  67.  
  68. static unsigned ScrnSeg;
  69. static int      VideoPage;             /* Active Video page for library */
  70. static int      SavedMode;             /* Video Mode when called */
  71. static char     Init_Called= 0;        /* Was initialization done? */
  72.  
  73. /***********************************************************/
  74. /* Functions in this file:                                 */
  75. /* pcwinit() - Initializes the library.                    */
  76. /* getpage() - Returns video page library function use.    */
  77. /* setpage() - Sets library video page                     */
  78. /* switchpage() - Uses BIOS to switch active video page    */
  79. /* getscrnseg() - Returns active screen segment.           */
  80. /* getpagesize()- Returns current size of video page       */
  81. /***********************************************************/
  82.  
  83.  
  84. /***********************************************************/
  85. /*                           PCWINIT                       */
  86. /*                                                         */
  87. /* This function must be called to use any of the quick    */
  88. /* screen writing functions or the window functions. This  */
  89. /* is because we have to determine some basic info about   */
  90. /* the video environment.                                  */
  91. /* If called with a zero value before any other PCW call   */
  92. /* the automatic exit will not be established.  Automatic  */
  93. /* exit is the default.                                    */
  94. /***********************************************************/
  95.  
  96. void pcwinit(int action) {
  97.  
  98.      union REGS regs;                       /* For video interrupts */
  99.  
  100.      Init_Called    = (char) TRUE;
  101.      ega_byte       = (char far *) MK_FP(0x0000, 0x0487);
  102.      regen_len      = (int  far *) MK_FP(0x0000, 0x044c);
  103.      ega_rows       = (char far *) MK_FP(0x0000, 0x0484);
  104.      _pcw_copyright = "PC Windows (c) Copyright 1989-90 by Stan Milam";
  105.  
  106.      regs.x.ax = 0x1a00;                         /* Check for VGA */
  107.      int86(0x10, ®s, ®s);                  /* Invoke BIOS */
  108.      if (regs.h.al == 0x1a) {                    /* If VGA BIOS supported */
  109.         _adaptor  = VGA;                         /* Tell the world about it */
  110.         CheckSnow = FALSE;                       /* Don't need this */
  111.         if (regs.h.bl >= 7) {                    /* Determine the monitor */
  112.            switch(regs.h.bl) {
  113.               case  7 :
  114.               case 11 :
  115.                    _monitor = MONO;
  116.                    ScrnSeg  = MONOSEG;
  117.                    break;
  118.               case  8 :
  119.               case 10 :
  120.               case 12 :
  121.                    _monitor = COLOR;
  122.                    ScrnSeg  = COLORSEG;
  123.                    break;
  124.            }
  125.         }
  126.         regs.x.ax = 0x1200;                      /* Get amount of video ram */
  127.         regs.h.bl = 0x10;
  128.         int86(0x10,®s,®s);                 /* Ask the BIOS */
  129.         switch(regs.h.bl) {
  130.           case 0 : _video_ram =  64; break;
  131.           case 1 : _video_ram = 128; break;
  132.           case 2 : _video_ram = 192; break;
  133.           case 3 : _video_ram = 256; break;
  134.         }
  135.      }
  136.  
  137.      if (!_adaptor) {                            /* If no VGA */
  138.         regs.x.ax = 0x1200;                      /* Check for EGA */
  139.         regs.h.bl = 0x10;
  140.         int86(0x10,®s,®s);                 /* EGA BIOS supported? */
  141.         if (regs.h.bl == 0x10) {                 /* Not EGA or VGA */
  142.            int86(0x11,®s,®s);              /* Get BIOS Equip List */
  143.            if ((regs.x.ax & 0x0030) == 0x0030) { /* Check for MONO */
  144.               ScrnSeg   = MONOSEG;
  145.               _adaptor  = MDA;
  146.               _monitor  = MONO;
  147.               _video_ram= 4;
  148.               CheckSnow = FALSE;
  149.            }
  150.            else {                                /* Must be CGA */
  151.               ScrnSeg   = COLORSEG;
  152.               _adaptor  = CGA;
  153.               _monitor  = COLOR;
  154.               CheckSnow = TRUE;
  155.               _video_ram= 16;
  156.            }
  157.         }
  158.         else {                                   /* Must be an EGA */
  159.            CheckSnow = FALSE;
  160.            _adaptor  = EGA;
  161.            switch(regs.h.bh) {                   /* Which Monitor? */
  162.              case 0 :                            /* Color */
  163.                 ScrnSeg = COLORSEG;
  164.                 _monitor= COLOR;
  165.                 break;
  166.              case 1 :                            /* Mono */
  167.                 ScrnSeg = MONOSEG;
  168.                 _monitor= MONO;
  169.                 break;
  170.            }
  171.            switch(regs.h.bl) {                   /* How Much memory? */
  172.              case 0 : _video_ram =  64; break;
  173.              case 1 : _video_ram = 128; break;
  174.              case 2 : _video_ram = 192; break;
  175.              case 3 : _video_ram = 256; break;
  176.            }
  177.         }
  178.      }
  179.      if (_adaptor > CGA) {                        /* Set alternate print */
  180.         regs.x.ax = 0x1200;                       /* screen to handle screen*/
  181.         regs.x.bx = 0x0020;                       /* sizes > 25 lines */
  182.         int86(0x10,®s,®s);
  183.      }
  184.      if (action) atexit(_pcw_exit);            /* Set up automatic exit */
  185.      vgetmode(&Vbump, &SavedMode, &VideoPage); /* Get initial info */
  186.      Vbump *= 2;                               /* Establish Vertical */
  187. }
  188.  
  189. /***********************************************************/
  190. /*                                                         */
  191. /* Now some miscellanious functions that will be called    */
  192. /* from the library functions to retrieve some of the      */
  193. /* values stored here in this file.                        */
  194. /*                                                         */
  195. /***********************************************************/
  196.  
  197. /***********************************************************/
  198. /*                      Chk_Video_State                    */
  199. /*                                                         */
  200. /* Determine if we are in a valid text mode to do screen   */
  201. /* writes.  Also return the number of colums & rows.       */
  202. /***********************************************************/
  203.  
  204.  
  205. int chk_video_state(int *max_rows, int *max_cols) {
  206.  
  207.    static int cols, mode, ap, rc;
  208.  
  209.    if (!Init_Called) pcwinit(1);
  210.    vgetmode(&cols, &mode, &ap);
  211.    Vbump = cols * 2;
  212.    rc = !((mode > 3 && mode < 7) || mode > 7);
  213.    if (_adaptor < EGA) *max_rows = 25;
  214.    else *max_rows = *ega_rows + 1;
  215.    *max_cols = cols;
  216.    return(rc);
  217. }
  218.  
  219.  
  220. /* Get the videopage the library is working in now  */
  221.  
  222. int getpage(void) {
  223.  
  224.     return(VideoPage);
  225. }
  226.  
  227. /* Get the Screen Memory Segment */
  228.  
  229. unsigned getscrnseg(void) {
  230.  
  231.    return(ScrnSeg);
  232. }
  233.  
  234. /* Return the pagesize to whoever wants to know */
  235.  
  236. int getpagesize(void) {
  237.  
  238.     return(*regen_len);
  239. }
  240.  
  241. /* Function to return Init_Called - Used to determine if   */
  242. /* initialization routine was called.  Library function    */
  243. /* will not work if it returns NULL                        */
  244.  
  245. int ispcwinit(void) {
  246.  
  247.     return((int) Init_Called);
  248. }
  249.  
  250. /***********************************************************/
  251. /*                         SetPage                         */
  252. /*                                                         */
  253. /* Set the video page for the library functions to work in.*/
  254. /* First edit to see if the specified page is legal.       */
  255. /* Return NULL if the specified page was illegal.          */
  256. /***********************************************************/
  257.  
  258. int setpage(int page) {
  259.  
  260.     int cols, mode, apage;
  261.  
  262.     if (!ispcwinit()) pcwinit(1);
  263.     if (page < 0 || _adaptor == MDA) return(0); /* No Mono here! */
  264.     vgetmode(&cols, &mode, &apage);         /* Get video Mode */
  265.     if (mode > 3) return(0);                /* Not a valid text mode */
  266.     if (mode < 2) {                         /* 40 column text mode */
  267.        if (_adaptor > CGA) {                /* When EGA we have... */
  268.           if (page > 15) return(0);         /* 15 pages-no more! */
  269.           else VideoPage = page;            /* Set the page */
  270.        }
  271.        else {                               /* Else we are CGA */
  272.           if (page > 7) return(0);          /* And we have 8 pages */
  273.           else VideoPage = page;
  274.        }
  275.     }
  276.     else {                                  /* Okay-80 column text mode */
  277.        if (_adaptor > CGA) {                /* And when its an EGA */
  278.           if (page > 7) return(0);          /* We have 8 pages */
  279.           else VideoPage = page;
  280.        }
  281.        else {                               /* And when its CGA */
  282.           if (page > 3) return(0);          /* 4 pages.         */
  283.           else VideoPage = page;
  284.        }
  285.     }
  286.     return(1);
  287. }
  288.  
  289. /***********************************************************/
  290. /*                         SwitchPage                      */
  291. /*                                                         */
  292. /* Use BIOS to switch the active display to another video  */
  293. /* page.  First edit to see if we can.                     */
  294. /***********************************************************/
  295.  
  296. void switchpage(int page) {
  297.  
  298.    union REGS regs;
  299.    int cols, mode, apage;
  300.  
  301.    if (!ispcwinit()) pcwinit(1);
  302.    if (page < 0 || _adaptor == MDA) return; /* No Mono here! */
  303.    vgetmode(&cols, &mode, &apage);          /* Get the video mode */
  304.    if (mode > 3) return;                    /* Not valid text mode */
  305.    if (mode < 2) {                          /* 40 Column text mode */
  306.       if (_adaptor > CGA) {                 /* If we are EGA we have... */
  307.          if (page > 15) return;             /* 15 page in 40 col text mode */
  308.       }
  309.       else                                  /* Otherwise...40 col CGA */
  310.          if (page > 7) return;              /* We have 8 pages */
  311.    }
  312.    else {                                   /* Otherwise...80 col text */
  313.       if (_adaptor > CGA) {                 /* If we are EGA... */
  314.          if (page > 7) return;              /* We have 8 pages to play with */
  315.       }
  316.       else                                  /* Otherwise...we are CGA and */
  317.          if (page > 3) return;              /* We have 4 */
  318.    }
  319.    regs.h.ah = 5;                           /* Call BIOS to switch */
  320.    regs.h.al = (char) page;                 /* To specified page */
  321.    int86(0x10, ®s, ®s);               /* BIOS Video */
  322. }
  323.